package com.maolong.helloworld;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    private static final String QUEUE_NAME = "hello";
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("182.92.151.172");
        factory.setUsername("maolong");
        factory.setPassword("123456");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        /**
         * 参数说明
         *  queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments)
         * queue 代表队列名称
         * durable 是否持久化
         * exclusive 是否独占，是否一个消费者监听一个队列
         * autoDelete 是否自动删除，是否自动删除。如果没有消费者consumer,
         * arguments 代表参数
         */
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        System.out.println("[producter] send message to queue: " + QUEUE_NAME);

        String message = "hello world";

//        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
//            String message = new String(delivery.getBody(), "UTF-8");
//            System.out.println(" [x] Received '" + message + "'");
//        };

        DeliverCallback deliverCallback = (consumerTag, message1) -> {
            String msg = new String(message1.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + msg + "'");
        };

        /**
         * 参数说明
         * basicConsume(String queue, boolean autoAck, DeliverCallback deliverCallback, CancelCallback cancelCallback)
         *   queue：队列名称
         *   autoAck：是否自动确认
         *   deliverCallback：消费回调对象
         *   cancelCallback：取消回调
         */
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
        });









    }
}
